package com.hfjyz.expert.expert.dao;

import java.util.List;

import com.hfjyz.base.bean.SearchCondition;
import com.hfjyz.base.dao.BaseDao;
import com.hfjyz.base.dao.PagingHibernateCallback;
import com.hfjyz.base.entity.ExpertEntity;
import com.hfjyz.expert.expert.bean.ExpertSearchBean;

public class ExpertDao extends BaseDao<ExpertEntity> {

    public ExpertDao() {
        super(ExpertEntity.class);
    }

    @SuppressWarnings("unchecked")
    public long countExpert(ExpertSearchBean expertSearch) {
        String hql = "select count(expert.id) from ExpertEntity as expert";

        Object[] values = null;
        if (expertSearch != null) {
            hql += expertSearch.getWhereClause();
            List<Object> paramList = expertSearch.getParamList();
            values = paramList.toArray();
        }
        List<Object> result = hibernateTemplate.find(hql, values);

        return (Long) result.get(0);
    }

    @SuppressWarnings("unchecked")
    public List<ExpertEntity> findExpert(SearchCondition cond, ExpertSearchBean expertSearch) {
        String hql = "from ExpertEntity ";

        Object[] values = null;
        if (expertSearch != null) {
            hql += expertSearch.getWhereClause();
            List<Object> paramList = expertSearch.getParamList();
            values = paramList.toArray();
        }
        hql += cond.getOrderString();

        PagingHibernateCallback callback = new PagingHibernateCallback(hql, cond.getPageNum(),
                cond.getRowCountPerPage(), values);
        List<ExpertEntity> result = hibernateTemplate.executeFind(callback);
        return result;
    }

    @SuppressWarnings("unchecked")
    public List<ExpertEntity> findExpertWithoutPage(SearchCondition cond, ExpertSearchBean expertSearch) {
        String hql = "from ExpertEntity ";

        Object[] values = null;
        if (expertSearch != null) {
            hql += expertSearch.getWhereClause();
            List<Object> paramList = expertSearch.getParamList();
            values = paramList.toArray();
        }
        hql += cond.getOrderString();

        List<ExpertEntity> result = hibernateTemplate.find(hql, values);
        return result;
    }

    @SuppressWarnings("unchecked")
    public ExpertEntity findExpertByIdNo(String idNo) {
        String hql = "from ExpertEntity e where e.idNo=?";
        List<ExpertEntity> result = hibernateTemplate.find(hql, idNo);
        if (result == null || result.size() == 0) {
            return null;
        } else {
            return result.get(0);
        }
    }

    @SuppressWarnings("unchecked")
    public long countExpertByIdNo(String idNo) {
        String hql = "select count(expert.id) from ExpertEntity as expert where idNo=?";

        List<Object> result = hibernateTemplate.find(hql, idNo);

        return (Long) result.get(0);
    }
}
